package along.com.myboxugu.fragment;


import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;

import android.support.constraint.solver.SolverVariable;
import android.support.constraint.solver.widgets.ConstraintAnchor;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Layout;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.lang.reflect.Type;
import java.util.ArrayList;

import along.com.myboxugu.R;
import along.com.myboxugu.activity.CourseModel;
import along.com.myboxugu.activity.News;
import along.com.myboxugu.activity.NewsActivity;

/**
 * A simple {@link Fragment} subclass.
 */
public class ExerciseFragment extends Fragment {

    RecyclerView recyclerView;
    ArrayList<News> lists=new ArrayList<>();


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
       View view =inflater.inflate(R.layout.fragment_exercise,container,false);
       recyclerView=view.findViewById(R.id.recyclerView11);
        nes();
        return view;
    }

    public  void  nes(){
        RequestParams params = new RequestParams("http://web.csmzxy.com/netCourse/readData");
        params.addQueryStringParameter("cmd", "7");
        params.addQueryStringParameter("v1", "274");
        params.addQueryStringParameter("v2", "1");
        params.addQueryStringParameter("v3", "30");

        x.http().get(params, new Callback.CommonCallback<String>() {
            Gson gson;
            @Override
            public void onSuccess(String result) {

                String result2 = convertString(result);
                Type listType= new TypeToken<ArrayList<News>>(){
                }.getType();
                if (gson == null){
                    gson = new Gson();

                }
                lists=gson.fromJson(result2,listType);
              //  mlistview.setAdapter(new MyAdatper());
                GridLayoutManager gridLayoutManager = new GridLayoutManager(getActivity(),1);
                recyclerView.setLayoutManager(gridLayoutManager);
                recyclerView.setAdapter(new MyAdatper());



            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
        }
    public String convertString(String result) {
        String result1 = result
                .replace(", responseSign : 00", "")
                .replace("\'", "\"")
                .replace("publishurl", "\"publishurl\"")
                .replace("webID", "\"webID\"")
                .replace("webname", "\"webname\"")
                .replace("webitemID", "\"webitemID\"")
                .replace("webitemname", "\"webitemname\"")
                .replace("websubitemname", "\"websubitemname\"")
                .replace("contentID", "\"contentID\"")
                .replace("contentAuthor", "\"contentAuthor\"")
                .replace("contentTitle", "\"contentTitle\"")
                .replace("submitTime", "\"submitTime\"")
                .replace("responseSign", "\"responseSign\"")
                .replace("websubitemID", "\"websubitemID\"");
        return result1;

    }
    public class  MyAdatper extends RecyclerView.Adapter<MyViewHolder>{

        @Override
        public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = View.inflate(getActivity(),R.layout.item_exercise,null);
           MyViewHolder myViewHolder =new MyViewHolder(view);
            return myViewHolder;
        }

        @Override
        public void onBindViewHolder(MyViewHolder holder, int position) {
            final News news=lists.get(position);
           holder.mTextView.setText(news.contentTitle);
            holder.timeText.setText(news.submitTime);
            holder.clickview.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent intent = new Intent(getActivity(), NewsActivity.class);

                    intent.putExtra("contentid",news.contentID);

                    startActivity(intent);
                }
            });
        }

        @Override
        public int getItemCount() {
            return lists.size();
        }
    }

    public  class  MyViewHolder extends RecyclerView.ViewHolder{
        TextView mTextView;
        TextView timeText;
        LinearLayout clickview;
        public MyViewHolder(View itemView) {
            super(itemView);
            mTextView=itemView.findViewById(R.id.item_tv);
            timeText=itemView.findViewById(R.id.date11);
            clickview=itemView.findViewById(R.id.clickview);
        }
    }



}
